package com.xudow.app.newui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.UserProfile;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.ui.ForgetPasswordActivity;
import com.xudow.app.ui.LoginActivity;
import com.xudow.app.ui.task.ModityPwdTask;

import java.util.HashMap;

import butterknife.BindView;

public class ModifyPwdActivity extends BaseUiActivity implements View.OnClickListener {



    @BindView(R.id.forg_pwd_tv)
    TextView forg_pwd_tv;

    @BindView(R.id.phone_et)
    EditText phone_et;

    @BindView(R.id.login_btn)
    TextView login_btn;

    @BindView(R.id.pwd1)
    EditText pwd1;//原密码

    @BindView(R.id.pwd2)
    EditText pwd2;//新密码

    @BindView(R.id.pwd3)
    EditText pwd3;//确认密码
    private UserProfile user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        user=getMyApp().getProfile();
        if(null!=user){
            phone_et.setText(user.getMobilePhone());
        }
    }

    @Override
    protected void initData() {

    }

    public void onBackBtnClick(View view){
        finish();
    }
    @Override
    protected void initListener() {
        forg_pwd_tv.setOnClickListener(this);

        login_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forg_pwd_tv:
                Intent intent = new Intent(this, ForgetPasswordActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.login_btn:
                String phone=phone_et.getText().toString().trim();
                String pwd_1=pwd1.getText().toString().trim();
                String pwd_2=pwd2.getText().toString().trim();
                String pwd_3=pwd3.getText().toString().trim();
                if (TextUtils.isEmpty(phone)){
                    phone_et.setError("请输入电话号码");
                    return;
                }
                if (TextUtils.isEmpty(pwd_1)){
                    pwd1.setError("请输入原始密码");
                    return;
                }
                if (TextUtils.isEmpty(pwd_2)){
                    pwd2.setError("请输入新密码");
                    return;
                }
                if (TextUtils.isEmpty(pwd_3)){
                    pwd3.setError("请确认新密码");
                    return;
                }
                if (pwd_1.equals(pwd_3)){
                    pwd2.setError("新密码不能与原密码相同");
                    return;
                }
                if (!pwd_2.equals(pwd_3)){
                    getMyApp().makeToast("新密码不一致");
                }
                HashMap<String,String> pramers=new HashMap<String, String>();
                pramers.put("new_password",pwd_2);
                pramers.put("old_password",pwd_1);
                pramers.put("phone",phone);
                ModityPwdTask task=new ModityPwdTask(this,handler);
                addTask(task);
                task.execute(pramers);
                showLodingDialog("正在处理");
                break;
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            hideLoadingDialog();
            if (msg.what==ModityPwdTask.SUCCESS){
                finish();
                Intent intent=new Intent(ModifyPwdActivity.this, LoginActivity.class);
                startActivity(intent);
            }
            getMyApp().makeToast((String)msg.obj);

        }
    };

}
